package com.itheima.CSM_bacK;

import com.itheima.common.constant.RedisConstant;
import com.itheima.common.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class MyTask{

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    AliOssUtil aliOssUtil;

    @Scheduled(cron = "0 0 0 * * ?")
    public void kkk(){
        Set DB_urls = redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_DB_RESOURCES).members();
        Set OSS_urls = redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_RESOURCES).members();

        Set kk = new HashSet(OSS_urls);
        kk.removeAll(DB_urls);
        System.out.println(kk.size());
        kk.forEach(e->{
            String url = (String) e;
            url = url.substring(url.lastIndexOf("/")+1);
            System.out.println(url);
            aliOssUtil.delete(url);
        });
        redisTemplate.delete(RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        redisTemplate.delete(RedisConstant.SETMEAL_PIC_RESOURCES);
        System.out.println("----------------");
    }
}
